package com.kuiandjie.web;


import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kuiandjie.entity.Goal;
import com.kuiandjie.entity.Task;
import com.kuiandjie.service.EmpTaskService;
import com.kuiandjie.service.GoalService;
import com.kuiandjie.service.TaskService;

public class TaskServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		String role = (String) session.getAttribute("role");
		
		if("leader".equals(role) || "member".equals(role)) {
			String curgid = request.getParameter("gid");
			
			if(curgid == null){
				response.sendRedirect("projectdetail.jspx");
			} else {
				
				
				TaskService ts = new TaskService();
				GoalService gs = new GoalService();
				
				Goal curgoal = gs.findGoalById(curgid);
				session.setAttribute("curgoal", curgoal);
				List<Task> tasklist = ts.findTaskByGid(curgid);
				//计算目标的进度
				int total = 0;
				int length = tasklist.size();
				float goalprogress = 0f;
				if(length != 0){
					for(Task t : tasklist){
						total += t.getProgress();
					}
					 goalprogress = (total*10/length)/10.0f;
				}
				request.setAttribute("goalprogress", goalprogress);
				
				EmpTaskService ets = new EmpTaskService();
				Map<Integer,String> map = ets.getEmpTaskMap(curgid);
				request.setAttribute("map", map);
				request.setAttribute("tasklist", tasklist);
				request.getRequestDispatcher("WEB-INF/views/task.jsp").forward(request, response);
			}
		} else {
			response.sendRedirect("main.jspx");
		}
	}
}
